#include <stdio.h>
#include "los_typedef.h"
#include "los_task.h"
#include "los_base.h"
#include "los_inspect_entry.h"
#include "watchdog.h"
#include <event.h>
#include "int.h"
#include <uctypes.h>
#include "uartx.h"

#include "los_api_dynamic_mem.h"
#define MEM_DYN_SIZE  4096
#define TEST_ALLOC_SIZE  1024
static UINT32 pDynMem[MEM_DYN_SIZE];
extern UINT32 LOS_MemInit(VOID *pPool, UINT32 uwSize);


static UINT32 g_uwDemoTaskID[12];
unsigned char g_mcache[4];
UINT32 uwRet = 0;
UINT8 Queue_CreateFlag = 0;
UINT8 MEMinit_CreateFlag = 0;
static UINT32 g_uwQueue;
UINT32 Test_Num = 0;
volatile UINT32 Test_Num1 = 0;
extern u8_t flash_cd_flag;
UINT32 FLASH_ADD_TEST=0x1FB000;
UINT32 FLASH_ADD_TEST2=0x1FB008;

void T_delay(UINT32 value)
{
    UINT32 i,j,k;
    for(i=0;i<value;i++)
        for(j=0;j<4000;j++)
		{
			if(Test_Num1<30000)
			{
				Test_Num1++;
			}
			else
			{
				Test_Num1=0;
			}
		}
}

void mcu_restart(void )
{
	PM_CTRL &= ~(1<<16);
	asm volatile ("nop");
	asm volatile ("nop");
	asm volatile ("nop");
	asm volatile ("nop");
	asm volatile ("nop");
	PM_CTRL |= (1<<16);
	asm volatile ("nop");
	asm volatile ("nop");
	asm volatile ("nop");
	asm volatile ("nop");
	asm volatile ("nop");
	PM_CTRL |= (1<<14);
}
#define MODE_ADDR 0x33ac00
void ISR_UART0(void)
{
	ICP |= 1<<23;
	volatile int app_mode;
	char *cmd = "ucchip_flashcd";
	char temp;
    if(uart_get_int_identity()&0x4)
	{
		for(int i = 0; i < 14; i++)
		{
			temp = uart_getchar();
			if(temp != cmd[i])
			{
				return;
			}
		}
		app_mode=0x3;
		//*(volatile int*)(MODE_ADDR)=0x3;
		download_flash(0x1c9000,&app_mode,4);
		mcu_restart();
    }
}
static VOID this_TskDeal(VOID)
{
    /* output a message on hyperterminal using printf function */
    while(1)
    {
    	printf("\r\nAll task start. 2021_05_10\r\n");
//        LOS_TaskYield();
        //LOS_Schedule();
    	LOS_TaskDelay(500);
		printf("\r\nTaskDelay ok\r\n");


    }
//    while(1);
//    run_test_task_001(1);
//    run_test_task_002(2);
}
//static VOID test1(VOID)
//{
//    /* output a message on hyperterminal using printf function */
//    while(1)
//    {
//    	printf("\r\nTask1 start-0.\r\n");
//
//    	(VOID)LOS_TaskDelay(1000);
//    }
//    while(1);
//}

//static VOID test2(VOID)
//{
//    /* output a message on hyperterminal using printf function */
//    while(1)
//    {
//    	printf("\r\nTask2 start-31.\r\n");
//
//    	(VOID)LOS_TaskDelay(1000);
//    }
//    while(1);
//}

static VOID test3(VOID)
{
	UINT32 uwRet;
    while(1)
    {
    	printf("\r\nTask3:Task4 Suspend.\r\n");
		LOS_TaskSuspend(g_uwDemoTaskID[3]);
		(VOID)LOS_TaskDelay(5000);
		printf("\r\nTask3:Task4 Resume.\r\n");
		LOS_TaskResume(g_uwDemoTaskID[3]);
		(VOID)LOS_TaskDelay(5000);
    }
}

static VOID test4(VOID)
{
    /* output a message on hyperterminal using printf function */
    while(1)
    {
    	printf("\r\nTask4 start-12.\r\n");
		if(MEMinit_CreateFlag==1)
		{
		    UINT32 *p_num4 = NULL;

		    /* mem alloc */
		    p_num4 = (UINT32 *)LOS_MemAlloc(pDynMem, TEST_ALLOC_SIZE);
		    if (NULL == p_num4)
		    {
		        dprintf("TASK4 mem alloc failed! \r\n");
		    }
			else
			{
				dprintf("TASK4 mem alloc %d ok, Addr is %0x \r\n",TEST_ALLOC_SIZE,p_num4 );
			}
			T_delay(1000);
		    /* mem free */
		    uwRet = LOS_MemFree(pDynMem, p_num4);
		    if (LOS_OK == uwRet)
		    {
		        dprintf("TASK4 mem free ok!\r\n");
		    }
		    else
		    {
		        dprintf("TASK4 mem free failed! \r\n");
		    }
		}
		(VOID)LOS_TaskDelay(400);
    }
}

static VOID test5(VOID)
{
	UINT32 uwReadbuf;
    /* output a message on hyperterminal using printf function */
    while(1)
    {
		for(uwReadbuf=0;uwReadbuf<10000;uwReadbuf++);
    	//printf("\r\nTask5 start5-2.\r\n");
// //       LOS_TaskYield();
////        LOS_Schedule();
//		if(Queue_CreateFlag==2)
//		{
//			/* read data from queue to uwReadbuf */
//			uwRet = LOS_QueueRead(g_uwQueue, &uwReadbuf, 24, 0);
//			if (uwRet != LOS_OK)
//			{
//				printf("recv message failure,error:%x \r\n",uwRet);
//				Queue_CreateFlag=1;
//			}
//			else
//			{
//				printf("recv message:%s \r\n", (CHAR *)uwReadbuf);
//				Queue_CreateFlag=1;
//			}
//			
//		}


    	(VOID)LOS_TaskDelay(20);
    }
    while(1);
}

static VOID test6(VOID)
{
    /* output a message on hyperterminal using printf function */
    while(1)
    {
    	printf("\r\nTask6 start-11.\r\n");
		T_delay(3000);
    	(VOID)LOS_TaskDelay(1000);
    }
    while(1);
}

static VOID test7(VOID)
{
    /* output a message on hyperterminal using printf function */
    while(1)
    {
    	printf("\r\nTask7 start-16.\r\n");
		T_delay(3000);
    	(VOID)LOS_TaskDelay(1000);
    }
    while(1);
}

static VOID test8(VOID)
{
	static CHAR abuf[] = "Queue TEST2";
	UINT32 uwlen = sizeof(abuf);
	UINT32 ota_uc=3;
	UINT32 i=0;
	static u32_t write_cnt = 0;
	UINT8 TEST_ARRY[200]={0};
	UINT8 TEST_ARRY2[4]={0x0,0x0,0x0,0x0};
    /* output a message on hyperterminal using printf function */
    while(1)
    {
    	//LOS_TaskLock();
		printf("\r\nTask8 start1-17.\r\n");
		
		for(i=0;i<200;i++)
		{
			TEST_ARRY[i]=i;
		}
		int_disable();
		printf("start1 INT disable success1! \r\n");
		
		if(0 == write_cnt)
		{
			FlashEnableWr();
			FlashEraseSector(FLASH_ADD_TEST);
			printf("start1 FlashEraseSector success1! \r\n");
		}
		FlashEnableWr();
		FlashPageProgram(FLASH_ADD_TEST2+write_cnt*200,TEST_ARRY,200);	 // set flash cd flag
		printf("start1 Flash Write 0X%X success1! \r\n",FLASH_ADD_TEST2+write_cnt*200);
		int_enable();

		//T_delay(3000);
		printf("\r\nTask8 start3-17.\r\n");
		//LOS_TaskUnlock();
//		//wdt_feed(UC_WATCHDOG);
//		//printf("wdt feed\r\n");
////        LOS_TaskYield();
////        LOS_Schedule();
//		if(Queue_CreateFlag>0)
//		{
//			Queue_CreateFlag=1;
//			/* write abuf data to queue */
//			uwRet = LOS_QueueWrite(g_uwQueue, abuf, uwlen, 0);
//			if(uwRet != LOS_OK)
//			{
//				printf("send message failure,error:%x \r\n",uwRet);
//			}
//			else
//			{
//				printf("\r\nSend message OK\r\n");
//				Queue_CreateFlag=2;
//			}
//		}
		write_cnt++;
		
		if(write_cnt >20)
				write_cnt = 0;			


		int_disable();
		memset(TEST_ARRY2, 0,4);
		FlashQRead(FLASH_ADD_TEST2,TEST_ARRY2,4);	 // set flash cd flag
		printf("start10 Flash Read success2!, Value: %x , %x , %x , %x \r\n",TEST_ARRY2[0],TEST_ARRY2[1],TEST_ARRY2[2],TEST_ARRY2[3]);
		int_enable();

		(VOID)LOS_TaskDelay(5000);
    }
    while(1);
}

static VOID test9(VOID)
{
    /* output a message on hyperterminal using printf function */
	UINT32 ota_uc=3;
	UINT8 TEST_ARRY[4]={0};
    while(1)
    {
    	//LOS_TaskLock();
		printf("\r\nTask9 start1-18.\r\n");
		
//		int_disable();
//		printf("start3 INT disable success1! \r\n");
//		printf("start3 FlashEraseSector success1! \r\n");
//		FlashEnableWr();
//		FlashEraseSector(0x2d900);
//		printf("start1 FlashEraseSector success1! \r\n");
//		FlashEnableWr();
//		FlashPageProgram(0x2c900,TEST_ARRY,4);	 // set flash cd flag
//		printf("start3 Flash Write success1! \r\n");
//		int_enable();
//		//T_delay(3000);
//		
//		printf("\r\nTask9 start2-18.\r\n");
//		
//		int_disable();
//		printf("start3 INT disable success2! \r\n");
//		FlashEnableWr();
////		FlashEraseSector(0x2c900);
////		printf("start3 FlashEraseSector success2! \r\n");
//		FlashEnableWr();
//		FlashPageProgram(0x2d900,TEST_ARRY,4);	 // set flash cd flag
//		printf("start3 Flash Write success2! \r\n");
//		int_enable();
		
		//T_delay(3000);
		printf("\r\nTask9 start3-18.\r\n");
 //       LOS_TaskYield();
//        LOS_Schedule();
    	
///////////////////queue TEST
		(VOID)LOS_TaskDelay(1000);
//		if(Queue_CreateFlag>0)
//		{
//			/* delete queue */
//			while (LOS_OK != LOS_QueueDelete(g_uwQueue))
//			{
//				(VOID)LOS_TaskDelay(1);
//			}
//		}
//		Queue_CreateFlag=0;
//		
//		if(flash_cd_flag==1)
//		{
//			int_disable();
//			uart_download();
//			uart_wait_tx_done();
//			mcu_restart();
//		}
//		
//		int_disable();
//		printf("INT disable success! \r\n");
//		FlashEnableWr();
//		FlashEraseSector(0x2c900);
//		printf("FlashEraseSector success! \r\n");
//		FlashEnableWr();
//		FlashPageProgram(0x2c900,TEST_ARRY,3);	 // set flash cd flag
//		printf("Flash Write success! \r\n");
//		int_enable();
//		printf("INT enable success! \r\n");
//		printf("delete the queue success! \r\n");
//		
//		(VOID)LOS_TaskDelay(10000);
    }
    while(1);
}

static VOID test10(VOID)
{
    /* output a message on hyperterminal using printf function */
	UINT8 TEST_ARRY2[4]={0x0,0x0,0x0,0x0};
    while(1)
    {
    	printf("\r\nTask10 start-19.\r\n");
		int_disable();
		memset(TEST_ARRY2, 0,4);
		FlashQRead(FLASH_ADD_TEST2,TEST_ARRY2,4);	 // set flash cd flag
		printf("start10 Flash Read success2!, Value: %x , %x , %x , %x \r\n",TEST_ARRY2[0],TEST_ARRY2[1],TEST_ARRY2[2],TEST_ARRY2[3]);
		int_enable();
		
		//T_delay(3000);
    	(VOID)LOS_TaskDelay(2000);
    }
    while(1);
}

static VOID test11(VOID)
{
    /* output a message on hyperterminal using printf function */
    while(1)
    {
    	printf("\r\nTask11 start-20.\r\n");
		osGetAllTskInfo();
		T_delay(3000);
    	(VOID)LOS_TaskDelay(5000);
    }
    while(1);
}



UINT32 run_every_task(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stTaskInitParam1,stTaskInitParam2,stTaskInitParam3,stTaskInitParam4,stTaskInitParam5,stTaskInitParam6,stTaskInitParam7,stTaskInitParam8,stTaskInitParam9,stTaskInitParam10,stTaskInitParam11;

	
	(VOID)memset((VOID *)(&stTaskInitParam3), 0, sizeof(TSK_INIT_PARAM_S));
    stTaskInitParam3.pfnTaskEntry = (TSK_ENTRY_FUNC)test3;
    stTaskInitParam3.uwStackSize =  0x1080;
    stTaskInitParam3.pcName = "InspectTsk3";
    stTaskInitParam3.usTaskPrio = 1;
    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[2], &stTaskInitParam3);
    if (uwRet != LOS_OK)
    {
        printf("\r\ntask3 not ok.\r\n");
        return LOS_NOK;
    }
	
	(VOID)memset((VOID *)(&stTaskInitParam4), 0, sizeof(TSK_INIT_PARAM_S));
    stTaskInitParam4.pfnTaskEntry = (TSK_ENTRY_FUNC)test4;
    stTaskInitParam4.uwStackSize =  0x1080;
    stTaskInitParam4.pcName = "InspectTsk4";
    stTaskInitParam4.usTaskPrio = 12;
    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[3], &stTaskInitParam4);
    if (uwRet != LOS_OK)
    {
        printf("\r\ntask4 not ok.\r\n");
        return LOS_NOK;
    }
//	
//	(VOID)memset((VOID *)(&stTaskInitParam5), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam5.pfnTaskEntry = (TSK_ENTRY_FUNC)test5;
//    stTaskInitParam5.uwStackSize = 0x1080;
//    stTaskInitParam5.pcName = "InspectTsk5";
//    stTaskInitParam5.usTaskPrio = 2;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[4], &stTaskInitParam5);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask5 not ok.\r\n");
//        return LOS_NOK;
//    }
//		
//	(VOID)memset((VOID *)(&stTaskInitParam6), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam6.pfnTaskEntry = (TSK_ENTRY_FUNC)test6;
//    stTaskInitParam6.uwStackSize = 0x1080;
//    stTaskInitParam6.pcName = "InspectTsk6";
//    stTaskInitParam6.usTaskPrio = 11;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[5], &stTaskInitParam6);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask6 not ok.\r\n");
//        return LOS_NOK;
//    }
//		
//	(VOID)memset((VOID *)(&stTaskInitParam7), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam7.pfnTaskEntry = (TSK_ENTRY_FUNC)test7;
//    stTaskInitParam7.uwStackSize = 0x2080;
//    stTaskInitParam7.pcName = "InspectTsk7";
//    stTaskInitParam7.usTaskPrio = 16;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[6], &stTaskInitParam7);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask7 not ok.\r\n");
//        return LOS_NOK;
//    }
//		
//	(VOID)memset((VOID *)(&stTaskInitParam8), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam8.pfnTaskEntry = (TSK_ENTRY_FUNC)test8;
//    stTaskInitParam8.uwStackSize = 0x2080;
//    stTaskInitParam8.pcName = "InspectTsk8";
//    stTaskInitParam8.usTaskPrio = 17;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[7], &stTaskInitParam8);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask8 not ok.\r\n");
//        return LOS_NOK;
//    }
//		
//	(VOID)memset((VOID *)(&stTaskInitParam9), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam9.pfnTaskEntry = (TSK_ENTRY_FUNC)test9;
//    stTaskInitParam9.uwStackSize = 0x2080;
//    stTaskInitParam9.pcName = "InspectTsk9";
//    stTaskInitParam9.usTaskPrio = 18;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[8], &stTaskInitParam9);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask9 not ok.\r\n");
//        return LOS_NOK;
//    }
//		
//	(VOID)memset((VOID *)(&stTaskInitParam10), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam10.pfnTaskEntry = (TSK_ENTRY_FUNC)test10;
//    stTaskInitParam10.uwStackSize = 0x2080;
//    stTaskInitParam10.pcName = "InspectTsk10";
//    stTaskInitParam10.usTaskPrio = 19;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[9], &stTaskInitParam10);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask10 not ok.\r\n");
//        return LOS_NOK;
//    }
//		
//	(VOID)memset((VOID *)(&stTaskInitParam11), 0, sizeof(TSK_INIT_PARAM_S));
//    stTaskInitParam11.pfnTaskEntry = (TSK_ENTRY_FUNC)test11;
//    stTaskInitParam11.uwStackSize = 0x2080;
//    stTaskInitParam11.pcName = "InspectTsk11";
//    stTaskInitParam11.usTaskPrio = 20;
//    uwRet = LOS_TaskCreate(&g_uwDemoTaskID[10], &stTaskInitParam11);
//    if (uwRet != LOS_OK)
//    {
//        printf("\r\ntask11 not ok.\r\n");
//        return LOS_NOK;
//    }
		
		
	/* create queue*/
	uwRet = LOS_QueueCreate("queue", 5, &g_uwQueue, 0, 24);
	if (uwRet != LOS_OK)
	{
		printf("create queue failure!,error:%x \r\n",uwRet);
	}
	else
	{
		Queue_CreateFlag=1;
	}

	printf("create the queue success! \r\n\n");
	
    return LOS_OK;
}


int main(void)
{
    
    UINT32 uwRet = LOS_OK;
    
    uwRet = LOS_KernelInit();
    if(uwRet != LOS_OK)
    {
        return LOS_NOK;
    }
	
	UART_CFG_Type uartx_cfg;
    uartx_cfg.Baud_rate = 115200;
    uartx_cfg.Parity = UART_PARITYBIT_NONE;//'N'
    uartx_cfg.Databits = UART_DATABIT_8;
    uartx_cfg.Stopbits = UART_STOPBIT_1;//1
    uartx_cfg.level = UART_TL_BYTE_14;

    uartx_init(UC_UART0, &uartx_cfg);

	printf("\r\nXXXLiteOSXXXX.\r\n");
	int flag = 0x8088;
	//IER |= 1<<23;//enable uart interrupt
    //int_enable();
	int_disable();
	printf("\r\nTEST:2022-07-19XXXX.\r\n");
	//printf("wdt begin\r\n");

    //wdt_init(UC_WATCHDOG, 1000);//1s
	//wdt_enable(UC_WATCHDOG);

    LOS_Inspect_Entry();		//
    //run_every_task();

    (void)LOS_Start();
    
	return 0;
}
